package com.coocaa.app.lib.cleaner.deleter.other.impl;

import android.content.Context;

import com.coocaa.app.lib.cleaner.deleter.other.OtherDataDeleterFactory;
import com.coocaa.app.lib.cleaner.deleter.utils.FileUtils;
import com.coocaa.app.lib.cleaner.mgr.DataHolder;

import java.util.List;

/**
 * Created by lu on 17-4-13.
 */

public class OtherDataDeleterImpl implements OtherDataDeleterFactory.OtherDataDeleter {
    @Override
    public void delete(Context context, final List<String> datas, final OtherDataDeleterListener listener) {
        DeleterThread.post(new Runnable() {
            @Override
            public void run() {
                listener.onOtherDeleteStart();
                if (datas != null && datas.size() > 0) {
                    long cleanSize = 0;
                    for (String path : datas) {
                        try {
                            long tempsize = FileUtils.getFileSize(path);
                            FileUtils.deleteFile(path);
                            cleanSize += tempsize;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (holder != null)
                            holder.removeOtherData(path);
                    }
                    if (cleanSize > 0) {
                        listener.onOtherDataDeleteEnd(true, cleanSize);
                        return;
                    }
                }
                listener.onOtherDataDeleteEnd(false, 0);

            }
        });
    }

    private DataHolder holder;

    @Override
    public void init(DataHolder holder) {
        this.holder = holder;
    }
}
